﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using PlaceOrder_Core.Helper;
using PlaceOrder_Core.PlaceOrderModels;
using PlaceOrder_Shared.Models.Common;
using static log4net.Appender.FileAppender;

namespace PlaceOrder_Core.Libraries.Postgresql;

public partial class IdbhostContext : DbContext
{
    public IdbhostContext()
    {
    }
    public static BaseConfigModel BaseConfig;
    public IdbhostContext(DbContextOptions<IdbhostContext> options)
        : base(options)
    {
       
    }

    public virtual DbSet<SysLogininfor> SysLogininfors { get; set; }
    public virtual DbSet<SysRole> SysRoles { get; set; }

    public virtual DbSet<UserTradeBondhelperLog> UserTradeBondhelperLogs { get; set; }

    public virtual DbSet<TradeSimulationUser> TradeSimulationUsers { get; set; }

    public virtual DbSet<ChatPrivilegeList> ChatPrivilegeLists { get; set; }

    public virtual DbSet<SysUser> SysUsers { get; set; }

    public virtual DbSet<ChatReceiver> ChatReceivers { get; set; }
    public virtual DbSet<UserTradeBondhelper> UserTradeBondhelpers { get; set; }

    public virtual DbSet<UserTradeVolumeConfig> UserTradeVolumeConfigs { get; set; }

    public virtual DbSet<TradeSimulationConfig> TradeSimulationConfigs { get; set; }

    public virtual DbSet<TradeSimulation> TradeSimulations { get; set; }

    public virtual DbSet<ChatMessage> ChatMessages { get; set; }

    public virtual DbSet<SysUserRole> SysUserRoles { get; set; }

    public virtual DbSet<ReviewMessage> ReviewMessages { get; set; }

    public virtual DbSet<Channel> Channels { get; set; }

    public virtual DbSet<Tstype> Tstypes { get; set; }

    public virtual DbSet<UserTradeDataBase> UserTrades { get; set; }

    public virtual DbSet<ClientVersion> ClientVersions { get; set; }
    #region 客户端相关实例
    public virtual DbSet<TradehistoryId22> TradehistoryId22s { get; set; }
    public virtual DbSet<TradehistoryId21> TradehistoryId21s { get; set; }
    public virtual DbSet<TradehistoryId20> TradehistoryId20s { get; set; }
    public virtual DbSet<TradehistoryId19> TradehistoryId19s { get; set; }
    public virtual DbSet<TradehistoryId18> TradehistoryId18s { get; set; }
    public virtual DbSet<TradehistoryId17> TradehistoryId17s { get; set; }
    public virtual DbSet<TradehistoryId16> TradehistoryId16s { get; set; }
    public virtual DbSet<TradehistoryId15> TradehistoryId15s { get; set; }
 //没有14？
    public virtual DbSet<TradehistoryId13> TradehistoryId13s { get; set; }
    public virtual DbSet<TradehistoryId12> TradehistoryId12s { get; set; }
    public virtual DbSet<TradehistoryId11> TradehistoryId11s { get; set; }
    public virtual DbSet<TradehistoryId10> TradehistoryId10s { get; set; }
    public virtual DbSet<TradehistoryId9> TradehistoryId9s { get; set; }
    public virtual DbSet<TradehistoryId8> TradehistoryId8s { get; set; }
    public virtual DbSet<TradehistoryId7> TradehistoryId7s { get; set; }
    public virtual DbSet<TradehistoryId6> TradehistoryId6s { get; set; }
    public virtual DbSet<TradehistoryId5> TradehistoryId5s { get; set; }
    public virtual DbSet<TradehistoryId4> TradehistoryId4s { get; set; }
    public virtual DbSet<TradehistoryId3> TradehistoryId3s { get; set; }
    public virtual DbSet<TradehistoryId2> TradehistoryId2s { get; set; }
    public virtual DbSet<TradehistoryId1> TradehistoryId1s { get; set; }
    public virtual DbSet<TradehistoryId0> TradehistoryId0s { get; set; }

    public virtual DbSet<Tsinfo> Tsinfos { get; set; }
    public virtual DbSet<Broker> Brokers { get; set; }
    public virtual DbSet<Holiday> Holidays { get; set; }

    public virtual DbSet<HengshengMarket> HengshengMarkets { get; set; }

    public virtual DbSet<Pricedifflist> Pricedifflists { get; set; }//价差

    public virtual DbSet<Pricedifflist2> Pricedifflist2s { get; set; }//碟式价差

    public virtual DbSet<TradeSimulationLog> TradeSimulationLogs { get; set; }

    public virtual DbSet<Setting> Settings { get; set; }

    public virtual DbSet<SysTradeAmountLimit> SysTradeAmountLimits { get; set; }

    public virtual DbSet<TradekchartId9> TradekchartId9s { get; set; }
    #endregion

    public static string SqlStr = null;
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        //0831
        if (SqlStr == null)
        {
            return;
        }

        if (!optionsBuilder.IsConfigured)
        {

            optionsBuilder.UseNpgsql(SqlStr,
               npgsqlOptions =>
               {
                   npgsqlOptions.EnableRetryOnFailure(
                       maxRetryCount: 5,
                       maxRetryDelay: TimeSpan.FromSeconds(30),
                       errorCodesToAdd: null);
               })
               .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

        }
    }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SysLogininfor>().HasNoKey(); //设置无主键
        modelBuilder.Entity<SysRole>().HasNoKey(); //设置无主键
        //modelBuilder.Entity<UserTradeBondhelperLog>().HasNoKey(); //设置无主键
        modelBuilder.Entity<TradeSimulationUser>().HasNoKey(); //设置无主键
        modelBuilder.Entity<ChatPrivilegeList>().HasNoKey(); //设置无主键
        modelBuilder.Entity<SysUser>().HasNoKey(); //设置无主键
        
        modelBuilder.Entity<ChatReceiver>().HasNoKey(); //设置无主键
        modelBuilder.Entity<UserTradeBondhelper>().HasKey(ut => ut.Id); //设置无主键
        modelBuilder.Entity<UserTradeBondhelperLog>()
       .HasKey(ut => ut.CreateTime);

        modelBuilder.Entity<ReviewMessage>()
               .Property(rm => rm.Id)
               .HasColumnName("id"); // 指定列名

        //modelBuilder.Entity<ReviewMessage>()
        //        .HasOne(rm => rm.UserTradeBondhelper)
        //        .WithMany()
        //        .HasForeignKey(rm => rm.UserTradeId);


    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

    private static readonly object _lock = new object();
    public bool IsDbConnectionOpen()
    {
        var connection = Database.GetDbConnection();
        try
        {
            lock (_lock)
            {
                if (connection.State != System.Data.ConnectionState.Open)
                {
                    connection.Open();
                }
                else
                {
                    return true;
                }
            }
        }
        catch (Exception ex)
        {
            AppLog.WriteError(ex + " 无法连接至数据库服务器。");
        }

        return connection.State == System.Data.ConnectionState.Open;
    }

}
